<ul class="stats">
    {{ if isset .Params "categories" }}
        {{ $categoryCount := (len .Params.sidebar.categories) }}

        <!--
            Set the title before displaying the categories associated with this post.
            The title will use the variables from the Categories menu set in the Config.
            If the Categories menu was not set then use the default values instead.
        -->
        {{ if ge $categoryCount 1 }}
            <li>
                {{ $categoryMenu := (where .Site.Menus.main "Name" "Categories") }}
                {{ if ne (len $categoryMenu) 0 }}
                    {{ $categoryMenu := index $categoryMenu 0 }}

                    {{ $.Scratch.Set "categoryUrl" $categoryMenu.URL }}

                    {{ with $categoryMenu.Identifier }}
                        <i class="{{ . }}">&nbsp;</i>
                    {{ end }}

                    {{ if gt $categoryCount 1 }}
                        {{ $categoryMenu.Name }}
                    {{ else }}
                        {{ $categoryMenu.Name | singularize }}
                    {{ end }}
                {{ else }}
                    {{ $.Scratch.Set "categoryUrl" "/categories/" }}

                    {{ if gt $categoryCount 1 }}
                        {{ i18n "categories" }}
                    {{ else }}
                        {{ i18n "category" }}
                    {{ end }}
                {{ end }}
            </li>
        {{ end }}
    {{ end }}

    <!-- Display the categories associated with this post -->
    {{ range .Params.sidebar.categories }}
        <li><a href="{{ add ($.Scratch.Get "categoryUrl") . | urlize | relLangURL }}" lang="{{ $.Lang }}">{{ . }}</a></li>
    {{ end }}
</ul>
